<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item><a href="/permission/admin">新闻管理</a></el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="margin: 0 0 20px 0;">
      <el-button type="primary" size="medium"
                 @click="$router.push('/news/newsDetail/add-new')">
        添加资讯
      </el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="title" label="题目"  align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="author" label="作者"  align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="shareCount" label="分享数"  align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="likeCount" label="点赞数"  align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="collectionCount" label="收藏数"  align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column label="查看详情" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="$router.push('/news/newsDetail/standard?id='+scope.row.id);">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" circle
                     @click="$router.push('/news/newsDetail/update?id='+scope.row.id);"></el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="text-align: right; margin: 10px auto;">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>

    <el-dialog title="编辑新闻" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="题目" prop="title">
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="editForm.author" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="正文" prop="content">
          <el-input v-model="editForm.content" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleListOptions: [],
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      editForm: {
        id:'',
        title:'',
        author: '',
        shareCount:'',
        likeCount:'',
        collectionCount:'',
        content:'',
      },
      rules: {
        title: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请输入作者', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        newsCategoryId: [
          {required: true, message: '请选择资讯分类', trigger: 'blur'},
        ],
        content: [
          {required: true, message: '请输入正文内容', trigger: 'blur'},
        ]
      },
      // 富文本编辑器
      editor: {}
    }
  },
  methods: {
    newsDetail(newsDetail) {

    },
    // 初始化富文本编辑器
    // initWangEditor() {
    //   this.editor = new this.wangEditor('#wangEditor');
    //   this.editor.config.zIndex = 1;
    //   this.editor.create();
    // },
    openEditDialog(newsDetail) {
      let url = 'http://localhost:9200/newsDetail/standard?id=' + newsDetail.id;

      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          // this.editor.txt.html(this.editForm.content);
          this.dialogFormVisible = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadNewsDetailList();
            }
          });
        }
      });
    },
    submitEditForm(){
      let url = 'http://localhost:9200/newsDetail/update';
      console.log('url = ' + url);


      let formData = this.qs.stringify(this.editForm);

      console.log("formData=" + formData)
      this.axios.post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state==20000){
          this.$message({
            message: '修改资讯成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadNewsDetailList();
        } else if (jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadNewsDetailList();
            }
          });
        }else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    openDeleteConfirm(newsDetail) {
      this.$confirm('此操作将永久删除【' + newsDetail.id + '-' + newsDetail.title + '】新闻，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(newsDetail);
      }).catch(() => {
      });
    },
    handleDelete(newsDetail) {
      let url = 'http://localhost:9200/newsDetail/delete';

      let formData = 'id=' + newsDetail.id;

      this.axios.post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除资讯成功！',
            type: 'success'
          });
          this.loadNewsDetailList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadNewsDetailList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    changePage(value) {
      this.$router.replace('?page=' + value);
      this.loadNewsDetailList();

    },
    loadNewsDetailList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9200/newsDetail/list?page=' + page;

       let localJwt = localStorage.getItem('localJwt');


      this.axios
          .create({
            'headers': {
              'Authorization':localJwt
            }
          })
            .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadNewsDetailList();
    // this.initWangEditor();
  }
}
</script>
